package com.hbmcc.heshenghuo.pager

import android.app.Activity
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.hbmcc.heshenghuo.R
import com.hbmcc.heshenghuo.databinding.FloorMyFrgPagerBinding
import com.hbmcc.heshenghuo.util.Formatter
import java.util.*

/**
 * @author 支豪亮
 * @date 2021/11/16 5:36 下午
 * @des 用来展示个人信息 个人、家庭、集团楼层的展示
 */
class FloorMyFrgPagerView(view: View) : ViewPager2.OnPageChangeCallback(), TabLayout.OnTabSelectedListener {

    private val dataBing = FloorMyFrgPagerBinding.bind(view)

    private lateinit var pagerAdapter: FloorMyFrgPagerAdapter

    private var isUpdate = false

    private var timer = Timer()

    private var list: List<FloorMyFrgPagerItem> = listOf(
        FloorMyFrgPagerItem("个人", listOf(
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "剩余话费", null))),
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "剩余流量", null))),
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "剩余语音", null))),
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "我的权益", null))),
        )),
        FloorMyFrgPagerItem("家庭", listOf(
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "宽带", null))),
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "电视", null))),
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "副卡", null))),
            FloorMyGridItem(listOf(FloorMyModelItem("--", "", "亲情号", null))),
        )),
        FloorMyFrgPagerItem("集团", null, FloorMyModelGroup(
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F53%2F0a%2Fda%2F530adad966630fce548cd408237ff200.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639645304&t=c3da3805d68a81ea9819391dccdcb4b1",
            "专属经理 智慧办公", "畅享多类集团权益", "立即加入")),

        )

    init {
        initTabLayout(list) //tab  初始化

        initPager(list) //pager 的初始化
        dataBing.notifyChange()
        dataBing.root.postDelayed({ updateData() }, 5000)

    }

    private fun updateData() {
        val list = listOf(
            FloorMyFrgPagerItem("个人", listOf(
                FloorMyGridItem(listOf(FloorMyModelItem("30.80", "元", "剩余话费", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量1")), FloorMyModelItem("100", "MB", "剩余流量", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量2")))),
                FloorMyGridItem(listOf(FloorMyModelItem("100", "MB", "剩余流量", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量3")))),
                FloorMyGridItem(listOf(FloorMyModelItem("90", "分钟", "剩余语音", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量4")))),
                FloorMyGridItem(listOf(FloorMyModelItem("10", "项", "我的权益", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量5")))),
            )),
            FloorMyFrgPagerItem("家庭", listOf(
                FloorMyGridItem(listOf(FloorMyModelItem("600", "M", "宽带", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量6")), FloorMyModelItem("爱家", "TV", "电视", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量7")))),
                FloorMyGridItem(listOf(FloorMyModelItem("爱家", "TV", "电视", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量8")))),
                FloorMyGridItem(listOf(FloorMyModelItem("1", "个", "副卡", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量9")))),
                FloorMyGridItem(listOf(FloorMyModelItem("1", "/3", "亲情号", FloorMyTripItem(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                    "您的流量已不足100MB，请及时补充流量", "充流量10")))),
            )),/*  FloorMyFrgPagerItem("中国", listOf(FloorMyGridItem(listOf(FloorMyModelItem("600", "M", "宽带", FloorMyTripItem(
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                "您的流量已不足100MB，请及时补充流量", "充流量")), FloorMyModelItem("爱家", "TV", "电视", FloorMyTripItem(
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                "您的流量已不足100MB，请及时补充流量", "充流量")))), FloorMyGridItem(listOf(FloorMyModelItem("爱家", "TV", "电视", FloorMyTripItem(
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                "您的流量已不足100MB，请及时补充流量", "充流量")))), FloorMyGridItem(listOf(FloorMyModelItem("1", "个", "副卡", FloorMyTripItem(
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202006%2F24%2F20200624135159_zcwoi.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639726501&t=4b75e2273322776296bfb0ee70d6e796",
                "您的流量已不足100MB，请及时补充流量", "充流量")))))),*/
            FloorMyFrgPagerItem("集团", null, FloorMyModelGroup(
                "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F53%2F0a%2Fda%2F530adad966630fce548cd408237ff200.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639645304&t=c3da3805d68a81ea9819391dccdcb4b1",
                "专属经理 智慧办公", "畅享多类集团权益", "立即加入")),
        )

        updateTableLayout(list)
        dataBing.notifyChange()
    }

    fun startTimer() {
        if (!isUpdate || list[dataBing.pagerInfo.currentItem].list.isNullOrEmpty() || list[dataBing.pagerInfo.currentItem].gideIndex == -1) return
        timer.cancel()
        timer = Timer()
        timer.schedule(TimerTask(), 5000, 5000)
    }

    fun stopTimer() {
        timer.cancel()
    }

    private fun updateTableLayout(list: List<FloorMyFrgPagerItem>?) {
        if (list.isNullOrEmpty() || list.size != 3) return

        isUpdate = true
        this.list = list
        for (index in list.indices) {
            list[index].gideIndex = 0
            val tab: TabLayout.Tab = dataBing.tabTitle.getTabAt(index) ?: continue
            (tab.customView as TextView).text = list[index].title
        }
        dataBing.tabTitle.getTabAt(0)?.select()

        updatePager(list)

        startTimer()
    }

    private fun updatePager(list: List<FloorMyFrgPagerItem>) {
        pagerAdapter.updateData(list)
    }

    private fun initTabLayout(list: List<FloorMyFrgPagerItem>) {
        for (item in list) {
            val tabView: View = LayoutInflater.from(dataBing.tabTitle.context).inflate(R.layout.floor_my_frg_tab_item, dataBing.tabTitle, false)
            val textView = tabView.findViewById<TextView>(R.id.tv_title_title_item)
            textView.text = item.title
            val tab: TabLayout.Tab = dataBing.tabTitle.newTab().setCustomView(tabView)
            dataBing.tabTitle.addTab(tab, false)
        }
        dataBing.tabTitle.getTabAt(0)?.select()
        dataBing.tabTitle.addOnTabSelectedListener(this)
    }

    private fun initPager(list: List<FloorMyFrgPagerItem>) {
        pagerAdapter = FloorMyFrgPagerAdapter(list, dataBing.pagerInfo,object : FloorMyFrgGideItemListener {
            override fun onUpdateIndex(pageIndex: Int, grideIndex: Int, view: View) {
                pagerAdapter.onUpdateIndex(pageIndex, grideIndex, view)
                startTimer()
            }

        })

        dataBing.pagerInfo.offscreenPageLimit = 3
        (dataBing.pagerInfo.getChildAt(0) as? RecyclerView)?.overScrollMode = View.OVER_SCROLL_NEVER ////去掉viewPager的阴影效果
        dataBing.pagerInfo.adapter = pagerAdapter
        dataBing.pagerInfo.registerOnPageChangeCallback(this)
    }

    /**
     * ViewPager2 翻页监听
     */
    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        if (dataBing.tabTitle.selectedTabPosition == position) return
        dataBing.tabTitle.getTabAt(position)?.select()
        startTimer()
    }

    /**
     * TabLayout 的选中监听
     */
    override fun onTabSelected(tab: TabLayout.Tab?) {
        val position: Int = tab?.position ?: return
        if (dataBing.pagerInfo.currentItem == position) return
        dataBing.pagerInfo.currentItem = position
        startTimer()
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
    }

    override fun onTabReselected(tab: TabLayout.Tab?) {
    }

    private inner class TimerTask : java.util.TimerTask() {
        override fun run() {
            (dataBing.pagerInfo.context as Activity).runOnUiThread {
                if (pagerAdapter.isStop()) timer.cancel()
                else pagerAdapter.updateFlip(dataBing.pagerInfo.currentItem)

            }

        }
    }
}